package com.longdaji.decoration.ui.goodscategory.content.list;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.GoodsBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class GoodsContentListPresenter extends RxPresenter<GoodsContentListContract.View> implements GoodsContentListContract.Presenter {
    private final static String TAG = GoodsContentListPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public GoodsContentListPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void getGoodsList(String cateId, String orderBy,String userId) {
        addSubscribe(mDataManager.getGoodsList(cateId, orderBy,userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsBean>() {

                    @Override
                    public void onSuccess(GoodsBean goodsBean) {
                        if (mView != null) {
                            if (goodsBean.getGoods() != null && goodsBean.getGoods().size() > 0) {
                                mView.showGoodsList(goodsBean.getGoods());
                            } else {
                                mView.showGoodsListNone();
                            }

                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }
}
